<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Excel\Action\DataImporting;
use App\Models\Express;
use App\Models\GroupOrder;
use App\Models\GroupShipment;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Arr;

class GroupShipmentController extends AdminController
{
    public function getModel($with = false)
    {
        return GroupShipment::query()
            ->when($with, function ($query, $with) {
                $query->with($with);
            })
           ;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make($this->getModel(['product', 'userAddress', 'user', 'express']), function (Grid $grid) {
            $grid->column('id')->sortable();
//            $grid->column('order_type');
//            $grid->column('order_id');

            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->column('product.name', '商品名称');
            $grid->column('method', '配送方式')->using([
                1 => '快递配送',
                2 => '同城配送',
            ]);
            $grid->column('express.name', '快递公司');
            $grid->column('express.code', '快递公司编号');
            $grid->column('tracking_number');
            $grid->column('user.name', '会员名');
            $grid->column('user.flag', '会员标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('userAddress.consignee_name', '收货人姓名');
            $grid->column('userAddress.phone', '收货人电话');
            $grid->column('address', '详细地址')->display(function () {
                $address = $this->userAddress;
                if ($address) {
                    return "{$address->province}{$address->city}{$address->district}{$address->address}";
                }
                return '该用户未绑定地址';
            })->limit(10);
            $grid->export(config('shipment.export_title'))->xlsx()->rows(function ($rows) {
                return $this->dataProcessing($rows);
            })->filename('拼团发货单_填写后4列上传_批量发货' . now()->format('YmdHis'));

            $grid->column('shipped_at');
            $grid->column('status')->using([
                0 => '待发货',
                1 => '已发货',
                2 => '确认收货',
            ]);
            $grid->column('remark')->editable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('order_type', '来源',[
                    1 => '拼单商品',
                    2 => '挂售提货',
                ]);
                $selector->select('status', '类型', [
                    0 => '待发货',
                    1 => '已发货',
                    2 => '确认收货',
                ]);
                $selector->select('method', '配送方式', [
                    1 => '快递发货',
                    2 => '同城配送',
                ]);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('order_type', '来源')->select([
                    1 => '拼单商品',
                    2 => '挂售提货',
                ]);
                $filter->equal('status')->select([
                    0 => '待发货',
                    1 => '已发货',
                    2 => '确认收货',
                ]);
                $filter->equal('method', '配送方法')->select([
                    1 => '快递发货',
                    2 => '同城配送',
                ]);
                $filter->like('user.telephone', '会员电话');
                $filter->equal('user.flag', '会员标签');
                $filter->like('order.order_no', '订单编号');
                $filter->like('tracking_number', '快递号');
                $filter->equal('order.product.name', '商品名称');
                $filter->equal('express.name', '快递公司')->select(
                    Express::query()->pluck('name', 'id')->toArray()
                );
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
            $grid->tools([
                DataImporting::make('导入数据')->setModel(GroupOrder::class),
            ]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupShipment(), function (Show $show) {
            $show->field('id');
            $show->field('order_type');
            $show->field('order_id');
            $show->field('express_id');
            $show->field('tracking_number');
            $show->field('user_id');
            $show->field('user_address_id');
            $show->field('shipped_at');
            $show->field('status');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make($this->getModel(), function (Form $form) {
            $form->display('id');
            $form->text('order_type');
            $form->text('order_id');
            $form->text('express_id');
            $form->text('tracking_number');
            $form->text('user_id');
            $form->text('user_address_id');
            $form->text('shipped_at');
            $form->text('status');
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    protected function dataProcessing($rows)
    {
        foreach ($rows as $index => &$row) {
            if ($row->userAddress) {
                $row['userAddress.consignee_name'] = $row->userAddress->consignee_name;
                $row['userAddress.phone'] = $row->userAddress->phone;
                $row['address'] = "{$row->userAddress->province}{$row->userAddress->city}{$row->userAddress->district}{$row->userAddress->address}";
                $row['method'] = Arr::get([
                    1 => '快递发货',
                    2 => '同城配送',
                ], $row['method'], $row['method']);
            }
        }
        return $rows;
    }
}
